local TankIt = LibStub("AceAddon-3.0"):NewAddon("TankIt", "AceConsole-3.0", "AceEvent-3.0")

local db
local AceDB = LibStub("AceDB-3.0")

TankIt:SetDefaultModuleState(false)

function TankIt:OnInitialize()
	local defaults = {
		profile = {
			disabledModules = {},
		},
	}
	self.db = AceDB:New("TankItDB", defaults)
	db = self.db.profile
	
	-- will be replaced with a real config
	self:RegisterChatCommand("tankit", "OnChatCommand", true)
end

function TankIt:OnEnable()
	self:RegisterEvent("PLAYER_REGEN_DISABLED", "EnableModules")
	self:RegisterEvent("PLAYER_REGEN_ENABLED", "DisableModules")
end

--function TankIt:OnDisable()
--end

function TankIt:OnModuleCreated(module)
	module.core = self
	if module.isPersistent and not db.disabledModules[module.name] then
		module:Enable()
	end	
end

-- basic slash command handling, in future AceConfig will be used
function TankIt:OnChatCommand(param)
	param = param:trim()
	if param:lower() == "show" then
		self:EnableModules()
	elseif param:lower() == "hide" then
		self:DisableModules()
	elseif param:find("^disable %w*$") then
		local module = param:match("^disable (%w*)%$")
		db.disabledModules[module] = true
	end
end

function TankIt:EnableModules(event)
	for name, module in self:IterateModules() do
		if not module.isPersistent and not db.disabledModules[name] then
			self:EnableModule(name)
		else
			if module.OnCombatStart then
				module:OnCombatStart()
			end
		end
	end
end

function TankIt:DisableModules(event)
	for name, module in self:IterateModules() do
		if not module.isPersistent then
			self:DisableModule(name)
		else
			if module.OnCombatStop then
				module:OnCombatStop()
			end
		end
	end
end

-- debug
--_G.TI = TankIt
